#!/usr/bin/env python
# -*- coding: utf-8 -*-

#
# This program is free software; you can redistribute it and/or modify  
# it under the terms of the GNU General Public License as published by  
# the Free Software Foundation; either version 2 of the License, or     
# (at your option) any later version.                                   
#                                                                         
# A copy of the license can be found in the license.txt file supplied   
# with this software or at: http://www.gnu.org/copyleft/gpl.html       
#


class   Event(object) :
    def __init__(self, request_id=None, response_to=None, source_queue=None, op=0, body=None) :
        self.request_id = request_id
        self.response_to = response_to
        self.source_queue = source_queue
        self.op = op
        self.body = body
        ## __init__()

    ####-----------------------------------------------------------####

    def __str__(self) :
        result = []
        result.append('Event ID=%s' % self.request_id)
        if self.response_to :
            result.append('REPLY_TO=%s' % self.response_to)
        result.append('OP=%s(%d)' % (NAME[self.op],self.op))

        return ' '.join(result)
        ## __str__()

    def __eq__(self, o) :
        if not isinstance(o, self.__class__) : return False

        if (self.request_id,self.response_to,self.source_queue,self.op,self.body) == (o.request_id,o.response_to,o.source_queue,o.op,o.body) :
            return True

        return False
        ## __eq__()

    def __repr__(self) :
        result = ['Event',]
        result.append('ID=%s' % self.request_id)
        result.append('response_to=%r' % self.response_to)
        result.append('source=%r' % self.source_queue)
        result.append('op=%r' % self.op)
        result.append('body=%s' % repr(self.body))

        return ' '.join(result)
        ## __repr__()

    ## class Event



EVENT_NEW_CELL_BORN   = 1001
EVENT_NEW_CELL_DEVICE = 1002
EVENT_DROP_CELL       = 1004


####---------------------------------------------------------------####

OP_UNKNOWN     = 0


OP_RESPONSE    = 1
OP_FAILURE     = 2

OP_SHUTDOWN    = 9

OP_ASSIGN_CELL = 10000
OP_CREATE_CELL = 10001
OP_SPLIT_CELL  = 10002

OP_INSERT      = 11001
OP_ERASE       = 11003
OP_FIND        = 11004

OP_INSERTUNIQUE = 11005
OP_UPDATE       = 11006
OP_UPSERT       = 11007

OP_TEST_ECHO   = 11999


# add cell metadata to route table on CellKeeper
ROUTE_ADD_CELL = 12001
# delete cell metadata from route table on CellKeeper
ROUTE_DEL_CELL = 12002

ROUTE_DIGEST   = 12003

ROUTE_GET      = 12004

ROUTE_DEMAND   = 12005 


TS_HEART_BEAT  = 13001


####---------------------------------------------------------------####

NAME = {
    OP_UNKNOWN     : 'Unknown',

    OP_RESPONSE    : 'Response',
    OP_FAILURE     : 'Failure',
    OP_SHUTDOWN    : 'Shutdown',

    OP_ASSIGN_CELL : 'AssignCell',
    OP_CREATE_CELL : 'CreateCell',
    OP_SPLIT_CELL  : 'SplitCell',

    OP_INSERT      : 'Insert',
    OP_ERASE       : 'Erase',
    OP_FIND        : 'Find',

    OP_INSERTUNIQUE : 'InsertUnique',
    OP_UPDATE       : 'Update',
    OP_UPSERT       : 'Upsert',

    OP_TEST_ECHO   : 'TestEcho',


    EVENT_NEW_CELL_BORN   : 'EventNewCellBorn',
    EVENT_NEW_CELL_DEVICE : 'EventNewCellDevice',
    EVENT_DROP_CELL       : 'EventDropCell',

    ROUTE_ADD_CELL        : 'RouteAddCell',
    ROUTE_DEL_CELL        : 'RouteDelCell',

    ROUTE_DIGEST          : 'RouteDigest',
    ROUTE_GET             : 'RouteGet',
    ROUTE_DEMAND          : 'RouteDemand',

    TS_HEART_BEAT         : 'TSHeartBeat',
}


